/*********************************************************************************
Name:  CasaGroupContactusController ()
Copyright © 2012  Lodha Group
======================================================
======================================================
Purpose:
-------
Visualforce page to replace standard web2lead page for web2lead in casabylodha.com
======================================================
======================================================
History
-------
VERSION    AUTHOR                DATE             DETAIL              
1.0 -   CG Dev Team          03/04/2012      INITIAL DEVELOPMENT  
*********************************************************************************/

public class CasaGroupContactusController {
 
            public String campaignName {get;set;}
            public String publisher {get;set;}
    
  
    
            Lead weblead;
            public String name {get;set;}
            
            public String email {get;set;}
            public String countryCode {get;set;}
            public String mobileNumber {get;set;}
            public String stdCode{get;set;}
            public String landLineNumber {get;set;}
            public String country {get;set;}
            public String city {get;set;}
            public String uscitystate{get;set;}
            public String cityText {get;set;}
            
            public String projectInterested {get;set;}           
            public String projectType {get;set;}
            public String rectypeId {get;set;}
            public String sourceofinformation {get;set;}
            
         
            public String selectedcountry{get;set;}
            public String selectedcountry1{get;set;}
            
            public List<SelectOption> selectproject{get;set;}
            public List<String> projectNames{get;set;}
            public String selectedproject{get;set;}
            public String ProjectId{get;set;}
            
            public Map<Id,project_name__c> projectlistmap{get;set;}        
            public String emailTemplateId{get;set;}
            private String AdminEmailId;
            public Boolean isinserted{get;set;}
            String EmailId;
            String Error;
            project_name__c p{get;set;}

            
            public CasaGroupContactusController()            
             {
            try{
            
                
                 isinserted=false;
                 getAllprojectname();
                 campaignName= ApexPages.currentPage().getParameters().get('campaign');
                 publisher= ApexPages.currentPage().getParameters().get('publisher');
                 projectlistmap= new Map<Id,project_name__c>([select id,Name,Record_Type__c,Project_Type__c from Project_name__c
                 where name!='Lodha Bellezza' AND name!='Unknown Project' AND name!='Generic' AND Project_Type__c='casa' AND Isactive__c=true]);
                 projectNames  = new List<String>();
                 
                  for(project_name__c project:projectlistmap.values()){
                  
                        projectNames.add(project.Name);
                    }
                    projectNames.sort();
                 
                  EmailId = WebToLeadPages__c.getInstance('CasabyLodha').Admin_Email__c;   
                  /**** GET EMAIL TEMPLATE ID TO SEND AS AUTO-RESPONSE TO CAPTURED LEAD ****/
                 emailTemplateId = [select id from EmailTemplate where developername=:WebToLeadPages__c.getInstance('CasabyLodha').Email_Template__c limit 1].Id;       
                 AdminEmailId = WebToLeadPages__c.getInstance('CasabyLodha').Admin_Email__c;
                 this.country='India';
        
                             
                  
                 
               
               }catch(Exception e){
                   sendErrorMessage(e);
               }
            }
 
            
    /*******************************************************************
        Purpose     : Populate the All ProjectNames in dropdown                        
        Parameters  : None
        Returns     : List of SelectOption of all ProjectName
    ********************************************************************/

            public List<SelectOption> getAllprojectname()
            
                {
                                selectproject=new List<SelectOption>();
                                selectproject.add(new SelectOption('None','-Select Project-'));
                               
                                try{                          
                                   if(projectlistmap!=null && !projectlistmap.isEmpty()){                                        
                                          for(String str:projectNames){                                                              
                                            for(project_name__c project:projectlistmap.values()){                                                  
                                                if(str == project.Name){
                                                       selectproject.add(new selectoption(project.Id,project.Name));                                                
                                                    }
                                                }                
                                            }
                                       
                                        }                                
                                }catch(Exception e){
                                    sendErrorMessage(e);
                                }
                            return selectproject;                
                }
                
             
    /*******************************************************************
        Purpose     : Save Lead                      
        Parameters  : None
        Returns     : None
    ********************************************************************/      
                
            
            
            public void submitLead()            
                {                                                       
                             weblead=new Lead();
                         
                     try
                        
                        {
                         if(publisher!=null)
                            {
                                webLead.WebBanner_Source__c=publisher;                               
                            }
                            if(CampaignName!=null)
                            {
                             weblead.Campaign_Name__c=CampaignName;
                             }
                        if(selectedproject!= null && selectedproject!= '' && !projectlistmap.isEmpty())
                            {
                                rectypeId = RecordTypeHelper.getRecordTypeId('Lead',projectlistmap.get(selectedproject).Record_Type__c);
                                projectType =  projectlistmap.get(selectedproject).Project_Type__c.touppercase();
     
                            }
                           
                            if(sourceofinformation!=null && sourceofinformation!='')
                            { 
                                weblead.Lead_Source_Detail__c=sourceofinformation;
                            }
                                        weblead.LeadSource='web';
                                        weblead.lastname = name;
                                        weblead.email =email;                                     
                                        weblead.ISD_Code__c =CountryCode;
                                        weblead.STD_Code__c = stdCode;                                       
                                        weblead.Recordtypeid = rectypeId;                                             
                                        weblead.Campaign_Name__c=CampaignName;                                                                                                
                                        weblead.Project_Name__c=selectedproject;                                      
                               if(country=='India')     
                                  {
                                        weblead.Mobile__c =mobileNumber;
                                        weblead.Phone = stdCode+landLineNumber;                                                 
                               if(ProjectType=='COMMERCIAL')
                                  {
                                        weblead.Country_O__c = Country;
                                        weblead.City_O__c=city;                
                                        weblead.Office_Phone__c = stdCode+landLineNumber; 
                                        weblead.Phone = '';            
                                  }else
                                      {
                                        weblead.Country_R__c = Country;
                                        weblead.City_R__c = city;            
                                      }                                    
                                   } 
                                else                                              
                                   {
                                        weblead.Mobile__c =countryCode +mobileNumber ;
                                        weblead.Phone =countryCode +stdCode +landLineNumber;                                                                                                  
                                   if(ProjectType=='COMMERCIAL')                                                      
                                     {
                                         weblead.Country_O__c = Country;                                                              
                                         weblead.City_O__c='Others';
                                         weblead.Specify_Other_for_City_O__c = cityText;                
                                         weblead.Office_Phone__c = countryCode+stdCode+landLineNumber; 
                                         weblead.Phone = '';                                             
                                     }
                                     else if(country == 'United States'){
                                         weblead.Country_R__c = Country; 
                                         weblead.City_R__c = 'Others';
                                         weblead.Specify_Other_for_City_R__c =uscitystate; 
                                         
                                    }else{
                                                                             
                                         weblead.Country_R__c = Country; 
                                         weblead.City_R__c = 'Others';
                                         weblead.Specify_Other_for_City_R__c =cityText;           
                                         }
                                    
                                         insert weblead; 
                                         isinserted=true; 
                                         sendMailtoLead(emailTemplateId,weblead.Id);                                                                                                              
                                    }                 
                             } catch(exception e){
                                isinserted=false;
                               Error=e.getmessage();
                                
                            }           
                          }  
     /*******************************************************************
        Purpose     : sendmail after lead is created                     
        Parameters  : None
        Returns     : None
    ********************************************************************/                                                              
                    
          private void sendMailtoLead(String templateId,String leadId){
            try{
                Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
                mail.setTemplateId(templateId);         
                mail.setTargetObjectId(leadId);   
                mail.setSenderDisplayName('Casa Group');
                Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });         
            }
            catch(exception e){
                sendErrorMessage(e);
            }
        }      
    
    /**********************************************************************
        Purpose     : SendErrormail if there is an exception               
        Parameters  : None
        Returns     : None
    ***********************************************************************/
    public void sendErrorMessage(Exception e){
        try{
            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
            String[] toAdd =  new String[]{'pradeep.kumar-l-g@capgemini.com'};
            mail.setToAddresses(toAdd);
            mail.setReplyTo('noreply@lodhagroup.com');
            mail.setSenderDisplayName('Lodha Group');
            mail.setSubject('Customer creation error');
            String htmlcontent;
            htmlcontent='<b>Exception:</b><br/>'+e+'<br/>';           
            mail.sethtmlbody(htmlcontent);
            mail.setBccSender(false);
            mail.setUseSignature(false);
            List<Messaging.SendEmailResult> results = Messaging.sendEmail(new Messaging.Email[]{ mail });
        }
        catch(Exception ex){
        
        }
    }   
    /************************************************************************
        Purpose     : SendErrormail if there is an error in inserting Lead                 
        Parameters  : None
        Returns     : None
    ***********************************************************************/
    public pagereference senderrmsg()
     {
     try{
     
            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
            String[] toAdd =  new String[]{'pradeep.kumar-l-g@capgemini.com'};
            mail.setToAddresses(toAdd);
            mail.setReplyTo('noreply@lodhagroup.com');
            mail.setSenderDisplayName('Lodha Group');
            mail.setSubject('Customer creation error');
            String htmlcontent;
            if(Error==null || Error=='')
                Error ='';
            htmlcontent='<b>Exception:</b><br/>'+Error+'<br/><b>Lead Details:</b><br/>';
            htmlcontent+='<b>Name:</b>'+weblead.name+'<br/><b>Email Id:</b>'+weblead.email+'<br/>';
            htmlcontent+='<b>Mobile:</b>'+weblead.Mobile__c+'<br/><b>Landline:</b>'+weblead.phone+'<br/>';
            htmlcontent+='<b>stdCode:</b>'+stdCode+'<br/><b>country:</b>'+country+'<br/>';
            htmlcontent+='<b>city:</b>'+city+'<br/><b>citytext:</b>'+citytext+'<br/>';
            htmlcontent+='<b>CountryCode:</b>'+CountryCode+'<br/><b>Landline:</b>'+stdCode+weblead.phone+'<br/>';
            htmlcontent+='<b>Mobile:</b>'+weblead.Mobile__c+'<br/>';            
            htmlcontent+='<b>ProjectName:</b>'+selectedproject+'<br/><b>ProjectType:</b>'+ProjectType+'<br/>';            
            htmlcontent+='<b>Campaign name:</b>'+webLead.Campaign_Name__c+'<br/><b>Banner:</b>'+webLead.WebBanner_Source__c+'<br/>';            
            mail.sethtmlbody(htmlcontent);
            mail.setBccSender(false);
            mail.setUseSignature(false);
            List<Messaging.SendEmailResult> results = Messaging.sendEmail(new Messaging.Email[]{ mail });
            }
            catch(exception e)
            {}
            return null;
     } 
     
     /************************************************************************
        Purpose     : Test Class                  
        Parameters  : None
        Returns     : None
    ***********************************************************************/
     static testmethod void testcasaleadcapture()    
        {
        
        /*
         * Test record for country India and projectType is commercial
         */
         
           CasaGroupContactusController casagroup=new CasaGroupContactusController();
           casagroup.name='test';
           casagroup.email='test@test.com';
           casagroup.country='India';
           casagroup.city='Mumbai';
           casagroup.projectType='COMMERCIAL';
           casagroup.mobileNumber='14725836954';
           casagroup.submitLead();
           casagroup.senderrmsg();
           
           
           /*
            * Test record for country India and projecttype Non commercial
            */
            
           CasaGroupContactusController casagroup1=new CasaGroupContactusController();
          
           casagroup1.selectedproject='Casa Bella';
           casagroup1.name='test';
           casagroup1.email='test@test.com';
           casagroup1.country='India';
           casagroup1.city='Mumbai';
           casagroup1.projectType='residential';
           casagroup1.mobileNumber='14725836954';
           casagroup1.sourceofinformation='Radio';
           casagroup1.submitLead();                                           
           casagroup1.senderrmsg();
          
           
           /*
            * Test record for country Non India and projectType is commercial
            */
           CasaGroupContactusController casagroup2=new CasaGroupContactusController();
           
           casagroup2.selectedproject='Casa Bella';
           casagroup2.name='test';
           casagroup2.email='test@test.com';
           casagroup2.country='France';
           casagroup2.city='test';
           casagroup2.projectType='COMMERCIAL';
           casagroup2.mobileNumber='14725836954';
           casagroup2.sourceofinformation='Radio';
           casagroup2.submitLead();                     
           casagroup2.senderrmsg();
           
           /*
            * Test record for country  Non India and projectType Non commercial
            */
           CasaGroupContactusController casagroup3=new CasaGroupContactusController();
          
           casagroup3.selectedproject='Casa Bella';
           casagroup3.name='test';
           casagroup3.email='test@test.com';
           casagroup3.country='France';
           casagroup3.city='Test';
           casagroup3.projectType='residential';
           casagroup3.mobileNumber='14725836954';
           casagroup3.submitLead();                       
           casagroup3.senderrmsg();
           
           CasaGroupContactusController casagroup4=new CasaGroupContactusController();
           
           casagroup4.name='';
           casagroup4.mobileNumber='';          
           casagroup4.submitLead();                     
           casagroup4.senderrmsg();
          
        }
               
}